package com.party.controller.sys;

import com.party.common.Page;
import com.party.model.Curriculum;
import com.party.model.SysUser;
import com.party.service.study.CurriculumService;
import com.party.service.sys.SysUserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 011926 on 2014/9/14.
 */
@Controller
@RequestMapping("/sys/example")
public class ExampleController
{
    @Resource
    SysUserService sysUserService;
    @Resource
    CurriculumService curriculumService;

    private int pageSize = 9;

    /**
     * 用户列表
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public ModelAndView index(
            HttpServletRequest request,
            HttpServletResponse response
    ) {

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/sys/example/demo1");
        return modelAndView;
    }

    @RequestMapping(value = "/query")
    @ResponseBody
    public Map<String,Object> query(HttpServletRequest req) {
        Map<String, Object> map = new HashMap<String, Object>();
        String pageIndexStr = req.getParameter("pageIndex");
        String passport = req.getParameter("passport");
        String realname = req.getParameter("realname");
        String regtime = req.getParameter("regtime");

        int pageIndex = 1;
        if (pageIndexStr != null && !"".equals(pageIndexStr)) {
            pageIndex = Integer.parseInt(pageIndexStr);
        }
        SysUser sysUser = new SysUser();
        sysUser.setPassport(passport);
        sysUser.setRealname(realname);
        sysUser.setRegtime(regtime);
        Page p = this.sysUserService.getUserList(sysUser, pageSize, pageIndex);

        map.put("pageCount",p.getTotalPageCount() );
        map.put("tableData",p.getResult());
        return map;
    }

    @RequestMapping(value = "/delete")
    @ResponseBody
    public Map<String,Object> delete(HttpServletRequest req) {
        String ids = req.getParameter("ids");
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("num",1);
        return map;
    }

    /**
     *表单例子
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/form", method = RequestMethod.GET)
    public ModelAndView form(
            HttpServletRequest request,
            HttpServletResponse response
    ) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/sys/example/form");
        return modelAndView;
    }

    @RequestMapping(value = "/submit")
    @ResponseBody
    public Map<String,Object> submit(HttpServletRequest req) {
        String name = req.getParameter("name");
        String radio = req.getParameter("radio");
        String checkName = req.getParameter("checkName");
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("msg","name="+name+",radio="+radio+",checkName="+checkName);
        return map;
    }

}
